package com.tools.web.work.tree;

import com.tools.common.object.Note;
import com.tools.web.work.dictionary.Item;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 默认的树形节点数据接口实现类
 * */
@Note("默认的树形节点数据接口实现类")
public class TreeItem<ID, V> extends Item<V> implements TreeNode<ID> {

    @Note("主键 ID")
    private ID id;

    @Note("父节点 ID")
    private ID parentId;

    @Note("子节点集合")
    private List<TreeItem<ID, V>> children;

    /* **************************************************************************************
     *
     *          构造器
     *
     * **************************************************************************************
     * */

    public TreeItem() {}

    public static <ID, V> TreeItem<ID, V> of() {
        return new TreeItem<>();
    }

    /* **************************************************************************************
     *
     *          Getter / Setter
     *
     * **************************************************************************************
     * */

    public ID getId() {
        return id;
    }

    @Override
    public ID idGet() {
        return id;
    }

    public void setId(ID id) {
        this.id = id;
    }

    public ID getParentId() {
        return parentId;
    }

    @Override
    public ID parentIdGet() {
        return parentId;
    }

    public void setParentId(ID parentId) {
        this.parentId = parentId;
    }

    public List<TreeItem<ID, V>> getChildren() {
        return children;
    }

    @Override
    public List<TreeItem<ID, V>> childrenGet() {
        return children;
    }

    public void setChildren(List<TreeItem<ID, V>> children) {
        this.children = children;
    }

    @Override
    public void addChild(Object child) {
        if (children == null) {
            this.children = new ArrayList<>();
        }
        this.children.add((TreeItem<ID, V>) child);
    }

    @Override
    public String toString() {
        return "TreeItem{" +
                "id=" + id +
                ", value=" + super.getValue() +
                ", label=" + super.getLabel() +
                ", parentId=" + parentId +
                ", children=" + children +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        TreeItem<?, ?> treeItem = (TreeItem<?, ?>) o;
        return Objects.equals(id, treeItem.id) && Objects.equals(parentId, treeItem.parentId) && Objects.equals(children, treeItem.children);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), id, parentId, children);
    }

    /* **************************************************************************************
     *
     *          链式方法
     *
     * **************************************************************************************
     * */

    public TreeItem<ID, V> id(ID id) {
        this.setId(id);
        return this;
    }

    public TreeItem<ID, V> parentId(ID parentId) {
        this.setParentId(parentId);
        return this;
    }

    public TreeItem<ID, V> value(V value) {
        this.setValue(value);
        return this;
    }

    public TreeItem<ID, V> label(String label) {
        this.setLabel(label);
        return this;
    }

    public TreeItem<ID, V> children(List<TreeItem<ID, V>> children) {
        this.setChildren(children);
        return this;
    }

}
